using System;
using System.IO;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
using Object = UnityEngine.Object;

namespace FAssetsSeparate
{
    public class FAssetsSeparateWindow : EditorWindow
    {
        /// <summary> 树图</summary>
        PrefabTreeView _treeView;

        /// <summary> 树图状态 </summary>
        [SerializeField] TreeViewState _treeViewState;

        [MenuItem("Window/FTools/Assets Separate", priority = 2000)]
        public static void Menu()
        {
            GetWindow<FAssetsSeparateWindow>("资产分离");
        }

        void OnEnable()
        {
            _treeViewState ??= new TreeViewState();
            _treeView ??= new PrefabTreeView(_treeViewState);
        }

        void OnGUI()
        {
            minSize = new Vector2(700, 350);

            var rect = new Rect(5, 5, position.width - 10, position.height - 40);
            UpdateTreeView(rect);
            _treeView.OnGUI(rect);

            if (_treeView.GetRows().Count < 1)
            {
                EditorGUILayout.HelpBox("拖拽添加需要分离的资产预设！", MessageType.Info);
                GUI.enabled = false;
            }
            else
            {
                GUI.enabled = true;
            }

            GUILayout.FlexibleSpace();

            if (GUILayout.Button("执行", GUILayout.Height(30)))
            {
                AssetDatabase.SaveAssets();
                PrefabAssetSeparate.Separate(_treeView.paths);
                AssetDatabase.Refresh();
                _treeView.Clear();
            }

            GUI.enabled = true;
        }

        /// <summary> 更新树图 </summary>
        void UpdateTreeView(Rect rect)
        {
            var objs = DragDropAreaMulti(rect, DragAndDropVisualMode.Copy);
            if (objs == null)
                return;

            foreach (var obj in objs)
            {
                var path = AssetDatabase.GetAssetPath(obj);
                var extension = Path.GetExtension(path);
                if (extension.Equals(".prefab", StringComparison.OrdinalIgnoreCase))
                    _treeView.AddPath(path);
            }

            _treeView.Reload();
        }

        /// <summary> 绘制一个可接收拖拽资源的区域 </summary>
        static Object[] DragDropAreaMulti(Rect _rect, DragAndDropVisualMode _dropVisualMode)
        {
            Event evt = Event.current;

            if (!_rect.Contains(evt.mousePosition))
                return null;

            Object[] temp = null;

            switch (evt.type)
            {
                case EventType.DragUpdated:
                case EventType.DragPerform:
                    DragAndDrop.visualMode = _dropVisualMode;
                    if (evt.type == EventType.DragPerform)
                    {
                        DragAndDrop.AcceptDrag();
                        temp = DragAndDrop.objectReferences;
                    }

                    Event.current.Use();
                    break;
            }

            return temp;
        }
    }
}